package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.model.mapper.DictDetailMapper;
import com.ttg.model.pojo.DictDetail;
import com.ttg.model.vo.DictDetailVo;
import com.ttg.web.dao.DictDetailDao;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author ZQ
 * @version 1.0
 * @Description
 * @date 2021/1/26 13:26
 */
@Service
public class DictDetailDaoImpl extends ServiceImpl<DictDetailMapper, DictDetail> implements DictDetailDao {

    @Override
    public Map<Integer, Map<String, String>> dictsSc2GxdMapByDictIds(List<Integer> dictIds) {
        if (CollectionUtils.isEmpty(dictIds)) {
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<DictDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(DictDetail::getDdDictId, dictIds);
        List<DictDetail> rsList = list(queryWrapper);
        if (CollectionUtils.isEmpty(rsList)) {
            return Collections.emptyMap();
        }
        Map<Integer, Map<String, String>> rsMap = new HashMap<>();
        for (DictDetail dictDetail : rsList) {
            Map<String, String> tempMap = rsMap.getOrDefault(dictDetail.getDdDictId(), new HashMap<>());
            tempMap.put(dictDetail.getDdScValue(), dictDetail.getDdGxdValue());
            rsMap.put(dictDetail.getDdDictId(), tempMap);
        }

        return rsMap;
    }

    @Override
    public Map<Integer, List<DictDetailVo>> listMapByDictIds(List<Integer> dictIds) {
        if (CollectionUtils.isEmpty(dictIds)) {
            return Collections.emptyMap();
        }
        LambdaQueryWrapper<DictDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(DictDetail::getDdDictId, dictIds).orderByAsc(DictDetail::getDdSort);

        List<DictDetail> rsList = list(queryWrapper);
        if (CollectionUtils.isEmpty(rsList)) {
            return Collections.emptyMap();
        }
        Map<Integer, List<DictDetailVo>> rsMap = new HashMap<>();
        for (DictDetail dictDetail : rsList) {
            List<DictDetailVo> temp = rsMap.getOrDefault(dictDetail.getDdDictId(), new ArrayList<>());
            temp.add(DictDetailVo.builder().ddLabel(dictDetail.getDdLabel()).ddGxdValue(dictDetail.getDdGxdValue()).ddSort(dictDetail.getDdSort()).build());
            rsMap.put(dictDetail.getDdDictId(), temp);
        }
        return rsMap;
    }
}
